package com.hefeng.example.service.impl;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hefeng.example.config.Constant;
import com.hefeng.example.domain.CustomerManualWorkOrder;
import com.hefeng.example.domain.DeviceAutomaticAlarmWorkOrder;
import com.hefeng.example.domain.WorkOrderDispositionResult;
import com.hefeng.example.mapper.CustomerManualWorkOrderMapper;
import com.hefeng.example.service.CustomerManualWorkOrderService;
import com.hefeng.example.service.DeviceAutomaticAlarmWorkOrderService;
import org.apache.ibatis.annotations.Param;
import org.hibernate.validator.internal.util.stereotypes.Lazy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CustomerManualWorkOrderServiceImpl extends ServiceImpl<CustomerManualWorkOrderMapper, CustomerManualWorkOrder> implements CustomerManualWorkOrderService {

    @Autowired
    @Lazy
    private CustomerManualWorkOrderMapper customerManualWorkOrderMapper;

    @Autowired
    @Lazy
    private DeviceAutomaticAlarmWorkOrderService deviceAutomaticAlarmWorkOrderService;

    @Autowired
    @Lazy
    private CustomerManualWorkOrderService customerManualWorkOrderService;

    /**
     * 设备工单code前缀
     */
    private final String DEVICE_WORK_PREFIX = "CW";


    /**
     * 根据工单id获取设备信息
     *
     * @param id
     * @return
     */
    @Override
    public List<Map> WorkDeviceInfo(Integer id) {
        List<Map> list = new ArrayList<>();
        list = customerManualWorkOrderMapper.WorkDeviceInfo(id);
        return list;
    }


    @Override
    public Page<CustomerManualWorkOrder> getDeviceList(CustomerManualWorkOrder customerManualWorkOrder, long current, long size) {
        LambdaQueryWrapper<CustomerManualWorkOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = (LambdaQueryWrapper<CustomerManualWorkOrder>) this.listMaps(Wrappers.lambdaQuery(new CustomerManualWorkOrder()).select(CustomerManualWorkOrder::getId, CustomerManualWorkOrder::getCreatedTime, CustomerManualWorkOrder::getWorkDescription, CustomerManualWorkOrder::getWorkName, CustomerManualWorkOrder::getWorkIsItOverdue, CustomerManualWorkOrder::getStatus));


        Page<CustomerManualWorkOrder> pagin = new Page<>(current, size, true);
        IPage<CustomerManualWorkOrder> selectResult = customerManualWorkOrderMapper.getDeviceList(pagin, queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 获取工单详情
     *
     * @param id
     * @return
     */
    @Override
    public List<Map> WorkOrderDetails(@Param(value = "id") String id) {
        List<Map> list = new ArrayList<>();

        list = customerManualWorkOrderMapper.WorkOrderDetails(id);

        return list;
    }

    @Override
    public List<Map> turnWork(@Param(value = "id")String id) {
        return customerManualWorkOrderMapper.turnWork(id);
    }

    @Override
    public List<Map> finishWork(String id) {
        return customerManualWorkOrderMapper.finishWork(id);
    }

    /**
     * 生成转派工单
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(CustomerManualWorkOrder customerManualWorkOrder) {
        //状态启用
        customerManualWorkOrder.setStatus(String.valueOf(1));

        customerManualWorkOrder.setCreatedTime(new Date());
        //默认逾期时间为7天
        DateTime expiration = DateUtil.offsetDay(new Date(), 7);
        customerManualWorkOrder.setWorkOverdueTime(expiration);
        //居民端
        customerManualWorkOrder.setWorkSource(String.valueOf(1));

        customerManualWorkOrder.setWorkStatus(1);
        customerManualWorkOrder.setWorkIsItOverdue(String.valueOf(0));
        //默认不派发
        customerManualWorkOrder.setDispatchStatus(1);

        customerManualWorkOrder.setWorkType(String.valueOf(4));

        /**获取工单code**/
        String workCode = DEVICE_WORK_PREFIX + IdUtil.getSnowflake(0, 0).nextIdStr();
        customerManualWorkOrder.setWorkCode(workCode);

        return customerManualWorkOrderMapper.insert(customerManualWorkOrder);

    }

    /**
     * 创建已转派结果表
     */
    @Override
    public int insertT(WorkOrderDispositionResult workOrderDispositionResult) {
        return customerManualWorkOrderMapper.insertresult(workOrderDispositionResult);
    }


    /**
     * 修改工单派发状态和工单状态
     */
    @Override
    public void updateWorkAndDispatchStatus(String workId, String workType, Integer workStatus, Integer dispatchStatus, String workOverdue, String deviceAlarmId) {
        if (ObjectUtil.isNotNull(workId)) {
            if (ObjectUtil.isNotEmpty(workType)) {
                switch (workType) {
                    /** 1 代表设备工单 */
                    case "1":
                        /** 修改工单 */
                        DeviceAutomaticAlarmWorkOrder deviceAutomaticAlarmWorkOrder = new DeviceAutomaticAlarmWorkOrder();
                        deviceAutomaticAlarmWorkOrder.setId(workId);

                        /** 工单状态;订单状态（ 1 未派单，2、已派单，3 正常处理中 4-已完成，5-超时完成，6-逾期未完成 */
                        if (ObjectUtil.isNotNull(workStatus)) {
                            deviceAutomaticAlarmWorkOrder.setWorkStatus(workStatus);
                        }

                        /** 派发状态 1，未派发，2-已派发 */
                        if (ObjectUtil.isNotNull(dispatchStatus)) {
                            deviceAutomaticAlarmWorkOrder.setDispatchStatus(dispatchStatus);
                        }

                        /** 逾期状态 (1-工单超时，逾期，0-未超时，未逾期) */
                        if (ObjectUtil.isNotNull(workOverdue)) {
                            deviceAutomaticAlarmWorkOrder.setWorkIsItOverdue(workOverdue);
                        }

                        deviceAutomaticAlarmWorkOrderService.updateDeviceAutomaticAlarmWorkOrder(deviceAutomaticAlarmWorkOrder);


                        break;

                    /** 2 代表用户工单 */
                    case "2":
                        CustomerManualWorkOrder customerManualWorkOrder = new CustomerManualWorkOrder();
                        customerManualWorkOrder.setId(workId);

                        /** 工单状态;订单状态（ 1 未派单，2、已派单，3 正常处理中 4-已完成，5-超时完成，6-逾期未完成 */
                        if (ObjectUtil.isNotNull(workStatus)) {
                            customerManualWorkOrder.setWorkStatus(workStatus);
                        }

                        /** 派发状态 1，未派发，2-已派发 */
                        if (ObjectUtil.isNotNull(dispatchStatus)) {
                            customerManualWorkOrder.setDispatchStatus(dispatchStatus);
                        }

                        /** 逾期状态 (1-工单超时，逾期，0-未超时，未逾期) */
                        if (ObjectUtil.isNotNull(workOverdue)) {
                            customerManualWorkOrder.setWorkIsItOverdue(workOverdue);
                        }

                        customerManualWorkOrderService.updateCustomerManualWorkOrder(customerManualWorkOrder);

                        break;
                    default:
                        break;
                }
            }
        }

    }

    /**
     * 更新用户手动工单
     *
     * @param
     */
    @Override
    public void updateCustomerManualWorkOrder(CustomerManualWorkOrder customerManualWorkOrder) {
        baseMapper.updateManual(customerManualWorkOrder);
    }

    @Override
    public void createWorkHandleResult(WorkOrderDispositionResult workOrderDispositionResult) {
//        /** 工单逾期时间 */
//        Date workOverdueTime = workOrderDispositionResult.getWorkOverdueTime();
//
//        /** 判断工单是否逾期 */
//        long between =System.currentTimeMillis()- workOverdueTime.getTime();

//        if(between >= 0) {
        /** 超时完成 */
//           workOrderDispositionResult.setIsItOverdue(Constant.STRING_1);
        /** 更新工单状态和逾期状态 */
        this.updateWorkAndDispatchStatus(workOrderDispositionResult.getWorkId(), workOrderDispositionResult.getWorkType(), Constant.INT_5, null, Constant.STRING_1, workOrderDispositionResult.getDeviceAlarmId());
//        }else {
//            /** 正常完成 */
//            workOrderDispositionResult.setIsItOverdue(Constant.STRING_0);
//            /** 更新工单状态和逾期状态 */
//            this.updateWorkAndDispatchStatus(workOrderDispositionResult.getWorkId(),workOrderDispositionResult.getWorkType(),Constant.INT_4,null,Constant.STRING_0,workOrderDispositionResult.getDeviceAlarmId());
//        }
    }

}
